package com.victor9ac.mashup.es;

import java.util.ResourceBundle;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;

import static org.elasticsearch.index.query.QueryBuilders.*;


public class ESReader {
	
	private static final int RESULT_SIZE = 25;

	private static final String WORD_BANNED = "ronaldo";

	String indexName;

	ResourceBundle rb;
    /**
     * The client of elasticsearch
     */
    protected Client client;
	
	public ESReader(Client client) {
		ResourceBundle rb = ResourceBundle.getBundle("elasticsearch");
		this.client = client;
		this.indexName = rb.getString("comment.index");
	}

	public SearchResponse read(String keyword,String type){

		BoolQueryBuilder queryBuilder = QueryBuilders
                .boolQuery()
                .must(termsQuery("keyword", keyword.toLowerCase()))
                .mustNot(fuzzyQuery("comment", WORD_BANNED));

        SearchResponse searchResponse = client.prepareSearch(indexName)
                .setQuery(queryBuilder)
                .setSize(RESULT_SIZE)
                .execute()
                .actionGet();
         
		
		
		return searchResponse;
	}

}
